<?php
/* 
 * ********* CVS HEADER ********* 
 * 
 * $RCSfile: admdownadd.php,v $ 
 * $Date: 2006/03/13 14:10:57 $ 
 * $Revision: 1.4 $ 
 * 
 * ********* CVS HEADER ********* 
 */
$L_modules = '';
$L_base_env = get_env_path();

require_once($L_modules . get_path_module("fonctions.fichiers"));
require_once("module/TPLN/TPLN.php");

$TPLN = new TPLN; 

if($fid!=''){
    if($action=="process"){
        if($fid==-1){
            $query="INSERT INTO rportal_download (filename,fileonserver,id_path_upload,droit,description) VALUES ('$filename','$fileonserver','$pathid','$droit','$description')";
            @rportalrequete($query) or die("ERREUR : $query");
        }else{
            $query="UPDATE rportal_download SET filename='$filename',droit='$droit',description='$description' WHERE id='$fid'";
            @rportalrequete($query) or rportalerreur("ERREUR : $query","?op=");
        }
        header("Location: ?op=admdownliste");
        exit;
    }
    if($fid!=-1){
        $query="SELECT rportal_download.*,rportal_upload_path.path FROM rportal_download,rportal_upload_path WHERE rportal_download.id='$fid' AND rportal_download.id_path_upload=rportal_upload_path.id";
        $result=rportalrequete($query) or rportalerreur("ERREUR : $query","?op=");
        $fileinfo=rportalligne($result);
        $filename = $fileinfo['filename'];
        $fileonserver = $fileinfo['fileonserver'];
        $path = $fileinfo['path'];
        $pathid = $fileinfo['id_path_upload'];
        $droit = $fileinfo['droit'];
        $fid = $fileinfo['id'];
        $description=$fileinfo['description'];
    }else{
        $filename=$file;
        $fileonserver=$file;
        $query="SELECT path,droit_download FROM rportal_upload_path WHERE id='$pathid'";
        $result=rportalrequete($query) or rportalerreur("ERREUR : $query","?op=");
        $pathinfo=rportalligne($result);
        $pathid=$pathid;
        $path = $pathinfo['path'];
        $droit = $pathinfo['droit_download'];
        $description='';
        $fid=-1;
    }
    $TPLN->Open($op_basepath.get_path("tpl")."admdownadd.html"); 
	$TPLN->IncludeFile("header",$L_base_env.$session['environnement']."/header.php");
	$TPLN->IncludeFile("footer",$L_base_env.$session['environnement']."/footer.php");
	
	require_once(get_path_module("admin"));
	$pc = new panneau_controle();
	$pc->calculRubriqueParFonction('admdownliste');
	$TPLN->Parse("panneau_controle",$pc->retourneEntete());
	
    $TPLN->Parse("filename",$filename);
    $TPLN->Parse("fileonserver",$fileonserver);
    $TPLN->Parse("path",$path);
    $TPLN->Parse("pathid",$pathid);
    $TPLN->Parse("description",$description);
    $TPLN->Parse("fid",$fid);
    $TPLN->Parse("action","?op=admdownadd");
    $query="SELECT groupe.* FROM groupe,hierarchie WHERE groupe.id=hierarchie.id_groupe AND hierarchie.id_groupe_dessus='".$session['id_groupe']."'";
    $result=rportalrequete($query) or die ("ERREUR<br>$query");
    while($row=rportalligne($result)) {
        $TPLN->Parse("bloc1.groupe",$row['nom']); 
        $TPLN->Parse("bloc1.valeur",$row['id']); 
        if($droit==$row['id']) $TPLN->Parse("bloc1.selected","selected"); 
        else $TPLN->Parse("bloc1.selected",""); 
        $TPLN->Loop("bloc1"); 
    } 
}
else if($fid==''){
    $TPLN->Open($op_basepath.get_path("tpl")."admdownaddliste.html"); 
	$TPLN->IncludeFile("header",$L_base_env.$session['environnement']."/header.php");
	$TPLN->IncludeFile("footer",$L_base_env.$session['environnement']."/footer.php");
    
    require_once(get_path_module("admin"));
	$pc = new panneau_controle();
	$pc->calculRubriqueParFonction('admdownliste');
	$TPLN->Parse("panneau_controle",$pc->retourneEntete());
    
    $query="SELECT id,nom,path,filetype FROM hierarchie,rportal_upload_path WHERE rportal_upload_path.droit=hierarchie.id_groupe AND hierarchie.id_groupe_dessus='".$session['id_groupe']."' AND rportal_upload_path.droit_download!='-1'";
    $result=rportalrequete($query) or rportalerreur("ERREUR : $query","?op=");
    if(rportalnbresult($result)==0){
        $TPLN->EraseBloc("path");
    }
    $class="liste_impaire"; 
    while($path=rportalligne($result)){
        $TPLN->Parse("path.repertoire",$path['nom']);
        $TPLN->Parse("path.path",$path['path']);
        $query = "SELECT fileonserver FROM rportal_download WHERE id_path_upload='".$path['id']."'";
        $alreadydone="";
        $result2=rportalrequete($query) or rportalerreur("ERREUR : $query","?op=");
        while($file=rportalligne($result2)){
            $alreadydone .= " ".$file['fileonserver']." ";
        }
        $curpath=$path['path'];
        $dir=dir($curpath);
        $isfile=0;
        while ($file = $dir->read()) {
            if($file != "." && $file != "..") {
                if (is_file($curpath.$file)) {
                    if(stristr($alreadydone," ".$file." ")==false && (($path['filetype']!='' && stristr($path['filetype'],file_ext($file))!=false) || $path['filetype']=='')){
                    $isfile=1;
                    $TPLN->Parse("path.bloc1.pathid",$path['id']); 
                    $TPLN->Parse("path.bloc1.fichier",$file); 
                    $TPLN->Parse("path.bloc1.taille",fsize(intval(filesize($curpath.$file)))); 
                    $TPLN->Parse("path.bloc1.icone",GetIcone(strtolower(file_ext($file)))); 
                    $TPLN->Loop("path.bloc1"); 
                    }
                }
            }
        } 
        if($isfile==0) $TPLN->EraseBloc("path.bloc1");
        $TPLN->Loop("path");
    }
}
$TPLN->Write();
?>